//k1编辑 2022.2.16

if (!isServer) exitWith {};
private ["_extractionPointNo"];
private ["_spawnMarkerName", "_extractionMarkerName", "_extractionMarkerName2", "_result", "_boat1", "_boat2", "_boat3", "_group1", "_group2", "_group3", "_waypoint"];

_extractionPointNo = _this select 0;
_spawnMarkerName = "drn_Escape_ExtracionBoatSpawnPos" + str _extractionPointNo;
_extractionMarkerName = "drn_Escape_ExtractionPos" + str _extractionPointNo;
_extractionMarkerName2 = "drn_Escape_ExtractionPos" + str _extractionPointNo + "_1";

private _dir = (getMarkerPos _spawnMarkerName) getDir (getMarkerPos _extractionMarkerName);
_result = [[((getMarkerPos _spawnMarkerName) select 0) + 80, ((getMarkerPos _spawnMarkerName) select 1), 0],_dir, (a3e_arr_extraction_boat select floor (random count a3e_arr_extraction_boat)), A3E_VAR_Side_Blufor] call BIS_fnc_spawnVehicle;
_boat1 = _result select 0;
_group1 = _result select 2;
Escape_extVeh1 = _boat1;
publicVariable "Escape_extVeh1";

_waypoint = _group1 addWaypoint [getMarkerPos _extractionMarkerName, 0];
_waypoint setWaypointSpeed "FULL";
_waypoint setWaypointBehaviour "CARELESS";
_waypoint setWaypointFormation "WEDGE";
_waypoint setWaypointStatements ["true", "vehicle this land 'GET IN'"];

[_boat1, ["<t color='#00ff00'>" + "撤离！撤离！", {
	Escape_vehGo_x1 = 1;
	publicVariable "Escape_vehGo_x1";
},"",-9933,false,false,"true","player in _target && {alive (driver _target)} && {!isPlayer (driver _target)} && {isNil 'Escape_vehGo_x1'} && {isNil 'Escape_vehGo_x2'}"]] remoteExec ["addAction", 0,true];

[_boat1, ["<t color='#00ff00'>" + "乘组离开载具", {
	Escape_leaveVehicle_x1 = 1;
	publicVariable "Escape_leaveVehicle_x1";
},"",-9933,false,false,"true","_target distance player < 8 && {vehicle player == player} && {alive (driver _target)} && {!isPlayer (driver _target)} && {isNil 'Escape_leaveVehicle_x1'} && {isNil 'Escape_vehGo_x2'}"]] remoteExec ["addAction", 0,true];

_t = time + 1;
waitUntil {time > _t};

_result = [[((getMarkerPos _spawnMarkerName) select 0), ((getMarkerPos _spawnMarkerName) select 1) + 80, 0], _dir, (a3e_arr_extraction_boat_escort select floor (random count a3e_arr_extraction_boat_escort)), A3E_VAR_Side_Blufor] call BIS_fnc_spawnVehicle;
_boat3 = _result select 0;
_group3 = _result select 2;
Escape_extVeh3 = _boat3;
publicVariable "Escape_extVeh3";

_waypoint = _group3 addWaypoint [getMarkerPos _extractionMarkerName, 0];
_waypoint setWaypointSpeed "FULL";
_waypoint setWaypointBehaviour "COMBAT";
_waypoint setWaypointFormation "WEDGE";
//_waypoint setWaypointType "LOITER";
//_waypoint setWaypointLoiterRadius 100;

_t = time + 1;
waitUntil {time > _t};

_result = [[((getMarkerPos _spawnMarkerName) select 0) - 80, ((getMarkerPos _spawnMarkerName) select 1), 0], _dir, (a3e_arr_extraction_boat select floor (random count a3e_arr_extraction_boat)), A3E_VAR_Side_Blufor] call BIS_fnc_spawnVehicle;
_boat2 = _result select 0;
_group2 = _result select 2;
Escape_extVeh2 = _boat2;
publicVariable "Escape_extVeh2";

_waypoint = _group2 addWaypoint [getMarkerPos _extractionMarkerName2, 0];
_waypoint setWaypointSpeed "FULL";
_waypoint setWaypointBehaviour "CARELESS";
_waypoint setWaypointFormation "WEDGE";
_waypoint setWaypointStatements ["true", "vehicle this land 'GET IN'"];

[_boat2, ["<t color='#00ff00'>" + "撤离！撤离！", {
	Escape_vehGo_x3 = 1;
	publicVariable "Escape_vehGo_x3";
},"",-9933,false,false,"true","player in _target && {alive (driver _target)} && {!isPlayer (driver _target)} && {isNil 'Escape_vehGo_x3'} && {isNil 'Escape_vehGo_x4'}"]] remoteExec ["addAction", 0,true];

[_boat2, ["<t color='#00ff00'>" + "乘组离开载具", {
	Escape_leaveVehicle_x2 = 1;
	publicVariable "Escape_leaveVehicle_x2";
},"",-9933,false,false,"true","_target distance player < 8 && {vehicle player == player} && {alive (driver _target)} && {!isPlayer (driver _target)} && {isNil 'Escape_leaveVehicle_x2'} && {isNil 'Escape_vehGo_x4'}"]] remoteExec ["addAction", 0,true];

diag_log format["fn_RunExtractionBoat: Extraction boats spawned: %1, %2 and %3",_boat1,_boat2,_boat3];

A3E_EvacHeli1 = _boat1;
publicvariable "A3E_EvacHeli1";
A3E_EvacHeli2 = _boat2;
publicvariable "A3E_EvacHeli2";
A3E_EvacHeli3 = _boat3;
publicvariable "A3E_EvacHeli3";

_group1 setGroupIdGlobal ["Dolphin One"];
_group2 setGroupIdGlobal ["Dolphin Two"];
_group3 setGroupIdGlobal ["Hammerhead"];


[_boat1] spawn {
	params["_boat"];
	_t = time + 5;
	waitUntil {time > _t};
	[driver _boat,"我们是来帮你撤离的, '双髻鲨'在提供掩护."] remoteExec ["sideChat",0];
};
_boatGuard = {
	params["_boat"];
	_t = time + 6;
	waitUntil {time > _t};
	private _msg = ["我们正在遭受攻击!","受到攻击!","我们处于火力范围内!","受到损伤!","我认为这个地方不安全!"];
	waitUntil {getDammage _boat > .1};
	if (alive (driver _boat)) then {[driver _boat,selectRandom _msg] remoteExec ["sideChat",0]};
	waitUntil {!alive _boat};
	[[A3E_VAR_Side_Blufor,"HQ"],format["%1 正在沉没!",groupId (group (driver _boat))]] remoteExec ["sideChat",0];
};

_extractionGuard = {
	params ["_boat1","_boat2","_boat3"];
	private ["_pos","_marker"];
	_pos = getmarkerpos a3e_var_Escape_ExtractionMarker;
	_t = time + 6;
	waitUntil {time > _t};
	waitUntil {!alive _boat1 && {!alive _boat2} && {!alive _boat3}};
	[[A3E_VAR_Side_Blufor,"HQ"],"所有的船都沉了!"] remoteExec ["sideChat",0];
	_marker = createMarker [format["failedExtraction_%1",time/60], _pos];
	_marker setMarkerType "hd_dot";
	_marker setMarkerSize [1,1];
	_marker setMarkerText "失效的Evac";
	_marker setMarkerColor "ColorRed";
	deletemarker a3e_var_Escape_ExtractionMarker;
	if (!isNil "Escape_ShrinkRing_xx") exitWith {};
	Escape_ShrinkRing_end = true;
};

[_boat1] spawn _boatGuard;
[_boat2] spawn _boatGuard;
[_boat3] spawn _boatGuard;
[_boat1,_boat2,_boat3] spawn _extractionGuard;

_t = time + 1;
waitUntil {time > _t};

// Verkar inte funka...
(driver _boat1) action ["LightOff", _boat1];
(driver _boat2) action ["LightOff", _boat2];
(driver _boat3) action ["LightOff", _boat3];

_pos = getMarkerPos _spawnMarkerName;
if (Escape_Extr_mak) then {
	if (isNil "make_Extr_xx") then [{
		make_Extr_xx = createMarker ["make_Extr_xx", _pos];
		make_Extr_xx setMarkerType "mil_pickup";
		make_Extr_xx setMarkerSize [1,1];
		make_Extr_xx setMarkerText "前往";
		make_Extr_xx setMarkerColor "ColorGreen";
	},{
		make_Extr_xx setMarkerPos _pos;
	}];
};

if (Escape_ShrinkRing == 1) then {[a3e_var_Escape_ExtractionMarkerPos,800] spawn Escape_greenCircle2_sv};
if (Escape_ShrinkRing == 2 && {Escape_Extr_mak}) then {Escape_ShrinkRing_xx = 1; [_pos,150] spawn Escape_greenCircle2_sv};

_check = false;
_check2 = false;
_t = time + 1;
waitUntil {
	if (time > _t) then {
		_Players = call A3E_fnc_GetPlayers - [Player_Server] - Escape_Players_enemy;
		_c = count _Players;
		if (isNil "Escape_ShrinkRing_xx" && {Escape_ShrinkRing == 1} && {Escape_Extr_mak} && {{_x distance2D _pos < 150} count _Players > 0}) then {Escape_ShrinkRing_xx = 1; [_pos,150] spawn Escape_greenCircle2_sv}; //
		if (!isNil "Escape_leaveVehicle_x1" && {isNil "Escape_vehGo_x2"}) then {
			Escape_vehGo_x2 = 1;
			publicVariable "Escape_vehGo_x2";
			_group1 leaveVehicle _boat1;
		};
		if (!isNil "Escape_leaveVehicle_x2" && {isNil "Escape_vehGo_x4"}) then {
			Escape_vehGo_x4 = 1;
			publicVariable "Escape_vehGo_x4";
			_group2 leaveVehicle _boat2;
		};
		if (!isNil "Escape_vehGo_x1" && {isNil "Escape_vehGo_x2"}) then {
			Escape_vehGo_x2 = 1;
			_waypoint = _group1 addWaypoint [getMarkerPos _spawnMarkerName, 0];
			_waypoint setWaypointSpeed "FULL";
			_waypoint setWaypointBehaviour "CARELESS";
		};
		if (!isNil "Escape_vehGo_x3" && {isNil "Escape_vehGo_x4"}) then {
			Escape_vehGo_x4 = 1;
			_waypoint = _group2 addWaypoint [getMarkerPos _spawnMarkerName, 0];
			_waypoint setWaypointSpeed "FULL";
			_waypoint setWaypointBehaviour "CARELESS";
		};
		_check = ({_x in _boat1 || {_x in _boat2} || {_x in _boat3}} count _Players == _c); 
		_check2 = (_c > 0 && {_check || {Escape_Extr_mak && {_x distance2D _pos < 150} count _Players == _c}});
		_t = time + 1;
	};
	_check2
}; //50

if (_check) then {
	_boat1 land "NONE";
	_boat2 land "NONE";
	_boat3 land "NONE";

	if (alive (driver _boat1) && {isNil "Escape_vehGo_x2"}) then [{
		[driver _boat1,"船上的人都在了? 好吧，让我们离开这里!"] remoteExec ["sideChat",0];
	},{
		if (alive (driver _boat2) && {isNil "Escape_vehGo_x4"}) then [{
			[driver _boat2,"船上的人都在了? 好吧，让我们离开这里!"] remoteExec ["sideChat",0];
		},{
			if (alive (driver _boat3)) then {
				[driver _boat3,"船上的人都在了? 好吧，让我们离开这里!"] remoteExec ["sideChat",0];
			};
		}];
	}];

	if (isNil "Escape_vehGo_x2") then {
		_waypoint = _group1 addWaypoint [getMarkerPos _spawnMarkerName, 0];
		_waypoint setWaypointSpeed "FULL";
		_waypoint setWaypointBehaviour "CARELESS";
	};

	if (isNil "Escape_vehGo_x4") then {
		_waypoint = _group2 addWaypoint [getMarkerPos _spawnMarkerName, 0];
		_waypoint setWaypointSpeed "FULL";
		_waypoint setWaypointBehaviour "CARELESS";
	};

	_t = time + 10;
	waitUntil {time > _t};

	_waypoint = _group3 addWaypoint [getMarkerPos _spawnMarkerName, 0];
	_waypoint setWaypointSpeed "FULL";
	_waypoint setWaypointBehaviour "CARELESS";

	_t = time + 10;
	waitUntil {time > _t};
};

_check = false;
_check2 = false;
_t = time + 1;
_pos = getMarkerPos _spawnMarkerName;
waitUntil {
	if (time > _t) then {
		_Players = call A3E_fnc_GetPlayers - [Player_Server] - Escape_Players_enemy;
		_c = count _Players;
		_c2 = {_x distance2D _pos < 150} count _Players;
		if (isNil "Escape_ShrinkRing_xx" && {Escape_ShrinkRing == 1} && {Escape_Extr_mak} && {_c2 > 0}) then {Escape_ShrinkRing_xx = 1; [_pos,150] spawn Escape_greenCircle2_sv}; //
		_check = (_c > 0 && {_c2 == _c});
		_check2 = (_check || {{vehicle _x distance2D _pos < 150 && {vehicle _x == _boat1 || {vehicle _x == _boat2} || {vehicle _x == _boat3}}} count _Players > 0});
		_t = time + 1;
	};
	_check2
};

["我们终于逃出来了"] call drn_fnc_CL_ShowTitleTextAllClients;
A3E_Task_Exfil_Complete = true;
publicvariable "A3E_Task_Exfil_Complete";
_t = time + 5;
waitUntil {time > _t};
_Players = call A3E_fnc_GetPlayers - [Player_Server] - Escape_Players_enemy;
_c = count _Players;
if (_check || {{vehicle _x == _boat1 || {vehicle _x == _boat2} || {vehicle _x == _boat3}} count _Players == _c}) then [{
	a3e_var_Escape_MissionComplete = true;
	publicVariable "a3e_var_Escape_MissionComplete";
},{
	a3e_var_Escape_MissionFailed_LeftBehind = true;
	publicVariable "a3e_var_Escape_MissionFailed_LeftBehind";
}];

